package com.macro.mall.sdk;

import com.google.gson.Gson;
import com.kuaidi100.sdk.api.QueryTrack;
import com.kuaidi100.sdk.core.IBaseClient;
import com.kuaidi100.sdk.pojo.HttpResult;
import com.kuaidi100.sdk.request.QueryTrackParam;
import com.kuaidi100.sdk.request.QueryTrackReq;
import com.kuaidi100.sdk.response.QueryTrackResp;
import com.kuaidi100.sdk.utils.SignUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @ClassName Kuaidi100
 * @Package com.macro.mall.sdk
 * @Description TODO
 * @Author wsx
 * @Date 2024/8/713:43
 * @Version V1.0
 * @Copyright yunshan
 */
@Component
public class Kuaidi100 {

    @Value("${kuaidi100.key}")
    private String key;

    @Value("${kuaidi100.customer}")
    private String customer;

    /**
     * 实时快递查询接口
     */
    public QueryTrackResp queryTrack(QueryTrackParam queryTrackParam) throws Exception{
        //需要参数
        //com:CompanyConstant.YT 查询的快递公司的编码，一律用小写字母
        /*
        SF = "shunfeng";
        ZT = "zhongtong";
        YT = "yuantong";
        HT = "huitongkuaidi";
        ST = "shentong";
        YD = "yunda";
        EMS = "ems";
        JD = "jd";
        ZJS = "zhaijisong";
        DB = "debangkuaidi";
        SS = "shansong";
        KFW = "kfw";
        */
        //num: 查询的快递单号， 单号的最大长度是32个字符
        //phone: 收件人或寄件人的手机号或固话
        QueryTrackReq queryTrackReq = new QueryTrackReq();
        String param = new Gson().toJson(queryTrackParam);
        queryTrackReq.setParam(param);
        queryTrackReq.setCustomer(customer);
        queryTrackReq.setSign(SignUtils.querySign(param,key,customer));
        IBaseClient baseClient = new QueryTrack();
        HttpResult execute = baseClient.execute(queryTrackReq);
        if (execute.getStatus() == HttpStatus.SC_OK && StringUtils.isNotBlank(execute.getBody())){
            return new Gson().fromJson(execute.getBody(), QueryTrackResp.class);
        }
        return null;
    }
}
